<template>
	<div class="bg-white">
		<Form :model="user" :label-width="80">
			<FormItem class="w-100" label="账号">
				<Input v-model="user.account" />
			</FormItem>
			
			<FormItem class="w-100" label="电话">
				<Input v-model="user.tel" />
			</FormItem>
			<FormItem class="w-100" label="身份证">
				<Input v-model="user.secId" />
			</FormItem>
			<FormItem class="w-100" label="角色">
				<div v-for="(item, key) in allRoles" :key="key">
					<Checkbox @on-change="roleChange(item.roleId)" v-model="item.has">{{item.name}}</Checkbox>
				</div>
			</FormItem>
			

			<FormItem class="text-right">
				<Button @click="submit" type="primary">提交</Button>
			</FormItem>
		</Form>
		
		<div>
			<Form  :label-width="180">
				<FormItem class="w-100" label="原密码">
					<Input type="password" v-model="password.password" />
				</FormItem>
				<FormItem class="w-100" label="新密码">
					<Input type="password" v-model="password.np" />
				</FormItem>
				<FormItem class="w-100" label="确认新密码">
					<Input type="password" v-model="password.cp" />
				</FormItem>
				<FormItem class="text-right">
					<Button @click="changePassword" type="primary">修改密码</Button>
				</FormItem>
			</Form>
		</div>
	</div>

</template>
<script>
	export default {
		props: {
			params: {
				type: Object,
				default: undefined,
			}
		},
		data() {
			return {
				user: {
					nickName: "",
					password: "",
				},
				userId: 0,
				roles: [],
				allRoles: [],
				password: {
					password: "",
					np: "",
					cp: ""
				}
			}
		},
		methods: {
			changePassword: function() {
				if (!this.password.password) {
					this.message.error("原密码不能为空");
					return;
				}
				if (!this.password.np) {
					this.message.error("新密码不能为空");
					return;
				}
				if (!this.password.cp) {
					this.message.error("确认密码不能为空");
					return;
				}
				this.password.userId = this.user.userId;
				
				this.http.put({
					url: "/user/cp",
					data: this.password
				}).then(res => {
					this.message.info("修改成功");
					this.password = {
						password: "",
						np: "",
						cp: "",
						userId: this.user.userId
					};
				});
			},
			submit: function() {
				let roles = [];
				for (let index in this.allRoles) {
					let roleItem = this.allRoles[index];
					if (roleItem.has) {
						roles.push(roleItem.roleId);
					}
				}
				// self.user.openId = 0;
				this.user.roles = JSON.stringify(roles);
				delete this.user.ct;
				delete this.user.ut;
				delete this.user.cu;
				delete this.user.uu;
				delete this.user.openId;
				this.http.put({
					url: "/user/info",
					data: this.user,
				}).then(() => {
					this.message.info("修改成功");
					this.$router.detail.close();
				});
			},
			init: function() {
				this.search();
			},
			searchUser: function() {

				this.http.get({
					url: "/user/info",
					param: {
						userId: this.user.userId
					}
				}).then(res => {
					this.user = res.user;
					this.user.roles = JSON.parse(this.user.roles);
					for (let jndex in this.allRoles) {
						let role = this.allRoles[jndex];
						role.has = false;
						for (let index in this.user.roles) {
							let roleItem = this.user.roles[index];
							if (roleItem == role.roleCode) {
								role.has = true;
								this.roles.push(role);
							}
						}
					}


				});
			},

			search: function() {
				this.http.get({
					url: "/role/all",
					param: {
						status: true,
					}
				}).then(res => {
					this.allRoles = res;
					console.log(res);
				}).then(() => {
					this.searchUser();
				});
			}

		},

		created: function() {},
		mounted: function() {
			this.user.userId = this.params.userId;
			this.init();
		}
	}
</script>

<style>
</style>